package com.gitee.huxing.course.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.huxing.course.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Description: kafka生产者
 * @ProjectName: gdca-platform
 * @Package: com.gdca.cert.Producer
 * @ClassName: Producer
 * @Author: huxing
 * @DateTime: 2021-08-23 下午2:06
 */
@Slf4j
@Component
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * @Description:    发送消息
     * @Author: huxing
     * @param topic     订阅主题
     * @param message   消息内容
     * @Date: 2021/8/23 下午2:55
     **/
    public void send(String topic, String message){
        MessageVO messageVO = new MessageVO();
        messageVO.setIndex("gdca");
        messageVO.setBody("数安时代科技股份公司::" + message);
        messageVO.setRequestTime(LocalDateTime.now().toString());
        kafkaTemplate.send(topic, JSONObject.toJSONString(messageVO));
        log.info("发送消息成功:: {}", JSONObject.toJSONString(messageVO));
    }
}
